/**
 * 
 */
package com.soccer.cms.common.utils.image;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.soccer.cms.modules.cms.entity.CropImage;

/**
 * @author zouqone
 * @date 2015年5月30日 下午11:53:41
 * 图片裁剪工具类
 */
public class ImageCropUtils {

	/**
	 * 
	 */
	public ImageCropUtils() {
		// TODO Auto-generated constructor stub
	}

	private BufferedImage image = null;

	public void load(File imageFile) throws IOException {
		image = ImageIO.read(imageFile);
	}

	public int getImageWidth() {
		return image.getWidth();
	}

	public int getImageHeight() {
		return image.getHeight();
	}

	//图片裁剪
	public void cutTo(int x,int  y,int tarWidth, int tarHeight) throws FileNotFoundException {
		if (image == null) {
			throw new FileNotFoundException(
					"image file not be load.please execute 'load' function agin.");
		}

		int iSrcWidth = getImageWidth(); // 得到源图宽
		int iSrcHeight = getImageHeight(); // 得到源图长

		// 如果源图片的宽度或高度小于目标图片的宽度或高度，则直接返回出错
		if (iSrcWidth < tarWidth || iSrcHeight < tarHeight) {
			
			throw new RuntimeException("source image size too small.");
		}

		// 先求源图和目标图的尺寸比例
		double dSrcScale = iSrcWidth * 1.0 / iSrcHeight;
		double dDstScale = tarWidth * 1.0 / tarHeight;

		// 先确定剪裁尺寸
		int iDstLeft, iDstTop, iDstWidth, iDstHeight;
		if (dDstScale > dSrcScale) { // 目标图片宽
			iDstWidth = iSrcWidth;
			iDstHeight = (int) (iDstWidth * 1.0 / dDstScale);
		} else { // 目标图片高
			iDstHeight = iSrcHeight;
			iDstWidth = (int) (iDstHeight * dDstScale);
		}
		iDstLeft = (iSrcWidth - iDstWidth) / 2;
		iDstTop = (iSrcHeight - iDstHeight) / 2;

		// 剪裁
		this.image = image.getSubimage(x, y, tarWidth, tarHeight);
		
	}

	/**
	 * 	//图片缩放 不生成新的图片
	 */
	public void zoomTo(int tarWidth, int tarHeight) {
		BufferedImage tagImage = new BufferedImage(tarWidth, tarHeight,
				BufferedImage.TYPE_INT_RGB); // 缩放图像
		Image image = this.image.getScaledInstance(tarWidth, tarHeight,
				Image.SCALE_SMOOTH);
		Graphics g = tagImage.getGraphics();
		g.drawImage(image, 0, 0, null); // 绘制目标图
		g.dispose();
		this.image = tagImage;

	}

	/**
	 * 保存
	 * @param fileName
	 * @param formatName
	 * @throws IOException
	 */
	public void save(String fileName, String formatName) throws IOException {
		// 写文件
		FileOutputStream out = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ImageIO.write(this.image, formatName, bos);// 输出到bos
			out = new FileOutputStream(fileName);
			out.write(bos.toByteArray()); // 写文件
		} catch (IOException e) {
			throw e;
		} finally {
			try {
				if (out != null)
					out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	
	/**
	 * //缩放图片 生成新的图片
	 */
	public static boolean zoomImage(String srcFile, String dstFile, int width,
			int height, String formatName) {
		try {
			ImageCropUtils zoom = new ImageCropUtils();
			zoom.load(new File(srcFile));
			zoom.zoomTo(width, height);
			zoom.save(dstFile, formatName);
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

	private static ImageCropUtils fromImageFile(File file) throws IOException {
		ImageCropUtils utils = new ImageCropUtils();
		utils.load(file);
		return utils;
	}

	/**
	 * 加载图片
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static ImageCropUtils load(String fileName) throws IOException {
		File file = new File(fileName);
		return fromImageFile(file);
	}
	
	public static boolean isAtImageArea(CropImage cropImage){
		//x left
		if(!compareImageSize(cropImage.getImageX(),cropImage.getSelectorX())){
			return false;
		}
		//y top
		if(!compareImageSize(cropImage.getImageY(),cropImage.getSelectorY())){
			return false;
		}
		
		//x right
		if(!compareImageSize(cropImage.getImageX(),cropImage.getSelectorX(),cropImage.getImageW(),cropImage.getSelectorW())){
			return false;
		}
		
		//y bottom
		if(!compareImageSize(cropImage.getImageY(),cropImage.getSelectorY(),cropImage.getImageH(),cropImage.getSelectorH())){
			return false;
		}
		return true;
	}
	
	private static boolean compareImageSize(Double imageY, Double selectorY) {
		// TODO Auto-generated method stub
		return false;
	}

	private static boolean compareImageSize(Double imageY, Double selectorY,
			Double imageH, Double selectorH) {
		// TODO Auto-generated method stub
		return false;
	}

	public static Boolean compareImageSize(String x1,String x2){
		Integer i1 = ImageCropUtils.parseInt(x1);
		Integer i2 = ImageCropUtils.parseInt(x2);
		return i1>i2?false:true;
	}
	
	public static Boolean compareImageSize(String x1,String x2,String y1, String y2){
		Integer i1 = ImageCropUtils.parseInt(x1);
		Integer i2 = ImageCropUtils.parseInt(x2);
		Integer t1 = ImageCropUtils.parseInt(y1);
		Integer t2 = ImageCropUtils.parseInt(y2);
		return (i1+t1)<(i2+t2)?false:true;
	}
	
//	public static void main(String[] args) {
//		String p =  "e:/090613221071ed5df7cf2bce72.jpg";
//		ImageCropUtils image , image2;
//		try {
//			image = ImageCropUtils.load(p);
//			//image.zoomImage(p, "e:/xxx.jpg", 500, 500, "jpg");
//			image.zoomTo(500, 500);
//			image.cutTo(50,50,100, 100);
//			image.save("e:/111.jpg", "jpg");
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}
	
	/**
	 * 将字符串转化为整型数字
	 * @param str
	 * @return
	 */
	public static Integer parseInt(String str){
		Long num = Math.round(Double.parseDouble(str));
		
		return num.intValue();
	}
	
}
